package com.umeox.quranplayer.ui.adapter

import android.annotation.SuppressLint
import android.graphics.drawable.AnimationDrawable
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.umeox.quranplayer.R
import com.umeox.quranplayer.repository.QuranAudioManager
import com.umeox.quranplayer.repository.model.QuranAudioBean
import com.yc.music.model.AudioBean
import com.yc.music.tool.BaseAppHelper

class MusicListAdapter :
    RecyclerView.Adapter<MusicListAdapter.ItemViewHolder>() {
    var itemClickListener: ItemClickListener? = null
    private var movieBeans = mutableListOf<QuranAudioBean>()

    private var curAudioTitle = ""
    private var handleFavoriteEvent = true

    var favoriteNameList = QuranAudioManager.favoriteNameList

    private  var  playingAnim: AnimationDrawable? = null

    fun setHandleFavoriteEvent(handle:Boolean) {
        handleFavoriteEvent = handle
    }

    fun setCurAudioTitle(title:String) {
        curAudioTitle = title
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ItemViewHolder {
        val itemView =
            LayoutInflater.from(parent.context).inflate(R.layout.item_music, parent, false)
        return ItemViewHolder(itemView)
    }

    override fun onBindViewHolder(holder: ItemViewHolder, position: Int) {
        val movieBean = movieBeans[position]
        holder.bind(movieBean)
        holder.itemView.setOnClickListener { itemClickListener?.onItemClicked(movieBean) }
    }

    @SuppressLint("NotifyDataSetChanged")
    fun setDataList(list: List<QuranAudioBean>) {
        movieBeans.clear()
        movieBeans.addAll(list)
        notifyDataSetChanged()
    }

    override fun getItemCount(): Int {
        return movieBeans.size
    }

    inner class ItemViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {

        private val mIvFavorite: ImageView = itemView.findViewById(R.id.iv_favorite)
        private val mIvPlaying: ImageView = itemView.findViewById(R.id.iv_playing)
        private val mTvTitleAr: TextView = itemView.findViewById(R.id.tv_title_ar)
        private val mTvTitleEn: TextView = itemView.findViewById(R.id.tv_title_en)
        private val mTvNumber: TextView = itemView.findViewById(R.id.tv_number)

        fun bind(movieBean: QuranAudioBean) {
            mIvFavorite.isSelected = favoriteNameList.contains(movieBean.title)
            playingAnim = mIvPlaying.getDrawable() as AnimationDrawable

            if (movieBean.title == curAudioTitle) {
                mTvNumber.visibility = View.INVISIBLE
                mIvPlaying.visibility = View.VISIBLE

                val playService = BaseAppHelper.get().musicService
                if (playingAnim?.isRunning == true && (playService != null && playService.isPausing)) {
                    playingAnim?.stop()
                }
                if (playingAnim?.isRunning == false && (playService != null && playService.isPlaying)) {
                    playingAnim?.start()
                }
            } else {
                mTvNumber.text = movieBean.title
                mTvNumber.visibility = View.INVISIBLE
                if (playingAnim?.isRunning == true) {
                    playingAnim?.stop()
                }
                mIvPlaying.visibility = View.INVISIBLE
            }

            mTvTitleAr.text = movieBean.nameAR
            mTvTitleEn.text = movieBean.nameEN

            mIvFavorite.setOnClickListener {
                if (handleFavoriteEvent) {
                    mIvFavorite.isSelected = QuranAudioManager.setFavorite(movieBean)
                }
            }
        }
    }

    interface ItemClickListener {
        fun onItemClicked(position: AudioBean)
    }
}